<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
  <!-- Don't use this in production: -->
  <script src="../其他/js/babel.min.js"></script>
  <style>
    .list {
      width: 200px;
      height: 150px;
      background-color: skyblue;
      overflow: auto;
    }
    .news {
      height: 30px;
    }
  </style>
</head>
<body>
  <div id="root"></div>

  <script type="text/babel">
    class NewsList extends React.Component {
      state = {newsArr: []}
      componentDidMount() {
        setInterval(() => {
          const { newsArr } = this.state
          const news = '新闻' + (newsArr.length + 1)
          this.setState({newsArr: [news, ...newsArr]})
        }, 1000)
      }
      getSnapshotBeforeUpdate() {
        return this.refs.list.scrollHeight
      }
      componentDidUpdate(preProps, preState, height) {
        this.refs.list.scrollTop += this.refs.list.scrollHeight - height
      }
      render() {
        return (
          <div className="list" ref="list">
            {
              this.state.newsArr.map((n, index) => {
                return <div key={index} className="news">{n}</div>
              })
            }
          </div>
        )
      }
    }
    ReactDOM.render(<NewsList/>, document.getElementById('root'))
  </script>
</body>
</html>